<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%activity}}".
 *
 * @property int $id
 * @property string $name 活动名称
 * @property string $banner banner
 * @property string other_entry 其他活动入口
 * @property string other_entry_url 其他活动入口链接
 * @property int $activity_catalog 活动类型
 * @property int $peoples 组团人数
 * @property int $start_user_type 开团用户类型 1全部 2新用户 3老用户
 * @property int $join_user_type 参团用户类型 1全部 2新用户 3老用户
 * @property string $start_time 开始时间
 * @property string $end_time 结束时间
 * @property string $goods 商品编号
 * @property string $description 活动说明
 * @property int $status 1:未开始 2:进行中 3:已抢光
 * @property string $store_id StroeId
 * @property string $share_title 分享标题
 * @property string $share_img 分享图片
 * @property string $share_remark 分享文案
 * @property string $share_url 分享链接
 *
 */
class Activity extends \yii\db\ActiveRecord
{
    public $pic,$pic1,$pic2;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%activity}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'start_time', 'end_time', 'start_user_type', 'join_user_type'], 'required'],
            [['activity_catalog', 'peoples', 'start_user_type', 'join_user_type',  'status'], 'integer'],
            [['start_time', 'end_time'], 'safe'],
            [['goods','description','share_img','share_remark','share_url'], 'string'],
            [['share_title'],'string','max'=> 100],
            [['name','banner','other_entry','other_entry_url','store_id'], 'string', 'max' => 255],
            [['pic'],'file','skipOnEmpty'=>false,'extensions'=>'png,jpg,jpeg,gif','when'=>function($model){
                return !empty($model->pic);
            },'whenClient'=>"function(attribute,value){
                return false;
            }"],
            [['pic1'],'file','skipOnEmpty'=>false,'extensions'=>'png,jpg,jpeg,gif','when'=>function($model){
                return !empty($model->pic1);
            },'whenClient'=>"function(attribute,value){
                return false;
            }"],
            [['pic2'],'file','skipOnEmpty'=>false,'extensions'=>'png,jpg,jpeg,gif','when'=>function($model){
                return !empty($model->pic2);
            },'whenClient'=>"function(attribute,value){
                return false;
            }"],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '活动名称',
            'banner' => 'banner',
            'other_entry'=>'其他活动入口',
            'other_entry_url'=>'其他活动入口链接',
            'activity_catalog' => '活动类型',
            'peoples' => '组团人数',
            'start_user_type' => '开团用户类型',
            'join_user_type' => '参团用户类型',
            'start_time' => '开始时间',
            'end_time' => '结束时间',
            'goods' => '商品编号',
            'description' => '活动说明',
            'status' => '状态',
            'pic'=>'Banner图',
             'pic1'=>'其他活动入口图片',
            'pic2'=>'分享图片',
            'store_id'=>"StoreId",
            'share_title'=>'分享标题',
            'share_img'=>'分享图片',
            'share_remark'=>'分享文案',
            'share_url'=>'分享链接',

        ];
    }
    public function upload(){
        if($this->validate('pic')){
//            $imgname='banner_'.date('YmdHis').rand(1000,9999);
//            $this->pic->saveAs(Yii::$app->basePath.'/web/uploads/banner/'.$imgname.'.'.$this->pic->extension);
//            return '/admin/uploads/banner/'.$imgname.'.'.$this->pic->extension;
            $savePath = 'wumaituangou/'.date('Ymd').'/banner/';
            $filename = uniqid().'.'.$this->pic->extension;

            $stream = fopen($this->pic->tempName, 'r+');
            if (!Yii::$app->awss3Fs->putStream($savePath . $filename, $stream)) {
                throw new yii\web\ServerErrorHttpException('File upload failed.');
            }
            @unlink($this->pic->tempName);
//            var_dump(getenv('OSS_FILE_ROOT') . $savePath . $filename);exit;
            return getenv('OSS_FILE_ROOT') . $savePath . $filename;


        }else{
            return false;
        }

    }
    public function upload1(){
        if($this->validate('pic1')){
//            $imgname='activity_'.date('YmdHis').rand(1000,9999);
//
//            $this->pic1->saveAs(Yii::$app->basePath.'/web/uploads/activity/'.$imgname.'.'.$this->pic1->extension);
//            return '/admin/uploads/activity/'.$imgname.'.'.$this->pic1->extension;
            $savePath = 'wumaituangou/'.date('Ymd').'/activity/';
            $filename = uniqid().'.'.$this->pic1->extension;

            $stream = fopen($this->pic1->tempName, 'r+');
            if (!Yii::$app->awss3Fs->putStream($savePath . $filename, $stream)) {
                throw new yii\web\ServerErrorHttpException('File upload failed.');
            }
            @unlink($this->pic1->tempName);
//            var_dump(getenv('OSS_FILE_ROOT') . $savePath . $filename);exit;
            return getenv('OSS_FILE_ROOT') . $savePath . $filename;
        }else{
            return false;
        }

    }
    public function upload2(){
        if($this->validate('pic2')){
//            $imgname='activity_'.date('YmdHis').rand(1000,9999);
//
//            $this->pic2->saveAs(Yii::$app->basePath.'/web/uploads/activity/'.$imgname.'.'.$this->pic2->extension);
//            return '/admin/uploads/activity/'.$imgname.'.'.$this->pic2->extension;
            $savePath = 'wumaituangou/'.date('Ymd').'/activity_share/';
            $filename = uniqid().'.'.$this->pic2->extension;

            $stream = fopen($this->pic2->tempName, 'r+');
            if (!Yii::$app->awss3Fs->putStream($savePath . $filename, $stream)) {
                throw new yii\web\ServerErrorHttpException('File upload failed.');
            }
            @unlink($this->pic2->tempName);
//            var_dump(getenv('OSS_FILE_ROOT') . $savePath . $filename);exit;
            return getenv('OSS_FILE_ROOT') . $savePath . $filename;
        }else{
            return false;
        }

    }
}
